const { getProvinceAll, getProvinceAll_cxq } = require("../../api/locationApi");

Page({
  data: {
    index: 0,
    multiArray: [["全部"], ["北京"], ["猪肉绦虫", "吸血虫"]],
    multiIndex: [0, 0, 0],
    token: "",
    ProvinceAllData: [], //所有的省份原始值
    ProvinceAllCxq: [], // 省市区的原始值
    pid: "", //省份id
    location: "",
  },

  // 获取所有的省份
  async getProvinceAllHandle() {
    const res = await getProvinceAll({ token: this.data.token });
    const provinceAllName = [];
    res.data.data.list.forEach((item) => {
      provinceAllName.push(item.name);
    });
    this.setData({
      "multiArray[0]": provinceAllName,
      ProvinceAllData: res.data.data.list,
    });
  },

  // 获取省份下的城市区
  async getProvinceAllCxqHandle(data) {
    const res = await getProvinceAll_cxq(data);
    const ProvinceAllCxqList = [];
    res.data.data.list.forEach((item) => {
      ProvinceAllCxqList.push(item.name);
    });
    this.setData({
      "multiArray[1]": ProvinceAllCxqList,
      ProvinceAllCxq: res.data.data.list,
    });
    console.log("123--->", this.data.multiArray);
    return ProvinceAllCxqList;
  },

  // 联级选择器点击确定事件
  bindMultiPickerChange: function (e) {
    console.log("picker发送选择改变，携带值为", e.detail.value);
    this.setData({
      multiIndex: e.detail.value,
    });
    console.log(
      "值是什么",
      this.data.ProvinceAllData[e.detail.value[0]].name,
      this.data.ProvinceAllCxq[e.detail.value[1]].name
    );
    let sheng = this.data.ProvinceAllData[e.detail.value[0]].name;
    let shi = this.data.ProvinceAllCxq[e.detail.value[1]].name;
    wx.setStorageSync("dizhi", {
      sheng,
      shi,
    });
    /* this.setData({
      location: this.data.ProvinceAllCxq[e.detail.value[1]].name,
    }); */
  },

  backLocation() {
    wx.switchTab({
      url: "../home/home?location=" + this.data.location,
    });
  },

  // 联级选择器改变事件
  bindMultiPickerColumnChange: function (e) {
    console.log("修改的列为", e.detail.column, "，值为", e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex,
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        console.log(
          "修改的是哪一个_id",
          this.data.ProvinceAllData[e.detail.value].id
        );
        // 城市区数据
        let provinceAllCxq = this.getProvinceAllCxqHandle({
          token: wx.getStorageSync("token"),
          pid: this.data.ProvinceAllData[e.detail.value].id,
        });
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = provinceAllCxq;
            data.multiArray[2] = ["猪肉绦虫", "吸血虫"];
            break;
          case 1:
            data.multiArray[1] = ["鱼", "两栖动物", "爬行动物"];
            data.multiArray[2] = ["鲫鱼", "带鱼"];
            break;
        }
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1:
        switch (data.multiIndex[0]) {
          case 0:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ["猪肉绦虫", "吸血虫"];
                break;
              case 1:
                data.multiArray[2] = ["蛔虫"];
                break;
              case 2:
                data.multiArray[2] = ["蚂蚁", "蚂蟥"];
                break;
              case 3:
                data.multiArray[2] = ["河蚌", "蜗牛", "蛞蝓"];
                break;
              case 4:
                data.multiArray[2] = [
                  "昆虫",
                  "甲壳动物",
                  "蛛形动物",
                  "多足动物",
                ];
                break;
            }
            break;
          case 1:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ["鲫鱼", "带鱼"];
                break;
              case 1:
                data.multiArray[2] = ["青蛙", "娃娃鱼"];
                break;
              case 2:
                data.multiArray[2] = ["蜥蜴", "龟", "壁虎"];
                break;
            }
            break;
        }
        data.multiIndex[2] = 0;
        break;
    }
    console.log(data.multiIndex);
    this.setData(data);
  },

  onLoad: function (options) {
    const token = wx.getStorageSync("token");
    this.setData({
      token,
    });
  },

  onReady: function () {},

  onShow: function () {
    this.getProvinceAllHandle();
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
});
